{% extends "base.html" %}

{% block title %}{{ post.title }} - 个人博客{% endblock %}

{% block content %}
{% if is_preview %}
<div class="alert alert-info alert-dismissible fade show" role="alert">
    <i class="fas fa-eye"></i> 这是文章预览模式，文章尚未发布。
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
{% endif %}
<div class="row">
    <!-- Main Content -->
    <div class="col-lg-8">
        <article class="card">
            <div class="card-body">
                <!-- Post Header -->
                <div class="mb-4">
                    <h1 class="card-title h2 mb-3">{{ post.title }}</h1>
                    
                    <div class="text-muted small mb-3">
                        <i class="fas fa-user"></i> {{ post.author.username }}
                        <i class="fas fa-calendar ms-3"></i> {{ post.created_at.strftime('%Y年%m月%d日') }}
                        <i class="fas fa-eye ms-3"></i> {{ post.view_count }} 次浏览
                        {% if post.category %}
                        <i class="fas fa-folder ms-3"></i> 
                        <a href="{{ url_for('main.category_posts', slug=post.category.slug) }}" 
                           class="text-decoration-none">{{ post.category.name }}</a>
                        {% endif %}
                    </div>
                    
                    {% if post.tags %}
                    <div class="mb-3">
                        {% for tag in post.tags %}
                        <a href="{{ url_for('main.tag_posts', slug=tag.slug) }}" 
                           class="badge bg-secondary text-decoration-none me-1">
                            {{ tag.name }}
                        </a>
                        {% endfor %}
                    </div>
                    {% endif %}
                </div>

                <!-- Post Content -->
                <div class="post-content">
                    {{ post.content_html | safe }}
                </div>

                <!-- Post Footer -->
                <div class="mt-4 pt-3 border-top">
                    <div class="row">
                        <div class="col-md-6">
                            <small class="text-muted">
                                最后更新：{{ post.updated_at.strftime('%Y年%m月%d日 %H:%M') }}
                            </small>
                        </div>
                        <div class="col-md-6 text-md-end">
                            <div class="btn-group" role="group">
                                <button type="button" class="btn btn-outline-secondary btn-sm" 
                                        onclick="shareToWeibo()">
                                    <i class="fab fa-weibo"></i> 微博
                                </button>
                                <button type="button" class="btn btn-outline-secondary btn-sm" 
                                        onclick="shareToWechat()">
                                    <i class="fab fa-weixin"></i> 微信
                                </button>
                                <button type="button" class="btn btn-outline-secondary btn-sm" 
                                        onclick="copyLink()">
                                    <i class="fas fa-link"></i> 复制链接
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </article>

        <!-- Comments Section -->
        {% if not is_preview %}
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-comments"></i> 评论 
                    <span class="badge bg-primary">{{ comments | length }}</span>
                </h5>
            </div>
            <div class="card-body">
                <!-- Comment Form -->
                {% if form %}
                <form method="POST" action="{{ url_for('main.add_comment', post_id=post.id) }}">
                    {{ form.hidden_tag() }}
                    <div class="row mb-3">
                        <div class="col-md-4">
                            {{ form.name(class="form-control", placeholder="姓名") }}
                        </div>
                        <div class="col-md-4">
                            {{ form.email(class="form-control", placeholder="邮箱") }}
                        </div>
                        <div class="col-md-4">
                            {{ form.website(class="form-control", placeholder="网站 (可选)") }}
                        </div>
                    </div>
                    <div class="mb-3">
                        {{ form.content(class="form-control", placeholder="写下你的评论...") }}
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-paper-plane"></i> 发表评论
                    </button>
                </form>
                {% endif %}

                <!-- Comments List -->
                <div class="mt-4">
                    {% if comments %}
                        {% for comment in comments %}
                        <div class="comment-item border-bottom py-3">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <div>
                                    <strong>
                                        {% if comment.guest_name %}
                                            {{ comment.guest_name }}
                                        {% elif comment.author %}
                                            {{ comment.author.username }}
                                        {% else %}
                                            匿名用户
                                        {% endif %}
                                    </strong>
                                    {% if comment.guest_website %}
                                    <a href="{{ comment.guest_website }}" target="_blank" 
                                       class="text-decoration-none ms-2">
                                        <i class="fas fa-external-link-alt"></i>
                                    </a>
                                    {% endif %}
                                </div>
                                <small class="text-muted">
                                    {{ comment.created_at.strftime('%Y-%m-%d %H:%M') }}
                                </small>
                            </div>
                            <div class="comment-content">
                                {{ comment.content | nl2br }}
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                    <div class="text-center text-muted py-4">
                        <i class="fas fa-comment-slash fa-2x mb-2"></i>
                        <p>还没有评论，来抢沙发吧！</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}
    </div>

    <!-- Sidebar -->
    <div class="col-lg-4">
        <!-- Related Posts -->
        {% if related_posts %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-bookmark"></i> 相关文章</h5>
            </div>
            <div class="card-body">
                {% for related_post in related_posts %}
                <div class="mb-3">
                    <a href="{{ url_for('main.post_detail', slug=related_post.slug) }}" 
                       class="text-decoration-none">
                        <h6 class="mb-1">{{ related_post.title }}</h6>
                    </a>
                    <small class="text-muted">
                        {{ related_post.created_at.strftime('%Y-%m-%d') }}
                    </small>
                </div>
                {% if not loop.last %}<hr>{% endif %}
                {% endfor %}
            </div>
        </div>
        {% endif %}

        <!-- Post Info -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-info-circle"></i> 文章信息</h5>
            </div>
            <div class="card-body">
                <ul class="list-unstyled mb-0">
                    <li class="mb-2">
                        <i class="fas fa-calendar text-primary"></i>
                        <strong>发布时间：</strong>{{ post.created_at.strftime('%Y年%m月%d日') }}
                    </li>
                    <li class="mb-2">
                        <i class="fas fa-user text-primary"></i>
                        <strong>作者：</strong>{{ post.author.username }}
                    </li>
                    <li class="mb-2">
                        <i class="fas fa-eye text-primary"></i>
                        <strong>浏览次数：</strong>{{ post.view_count }}
                    </li>
                    {% if post.category %}
                    <li class="mb-2">
                        <i class="fas fa-folder text-primary"></i>
                        <strong>分类：</strong>
                        <a href="{{ url_for('main.category_posts', slug=post.category.slug) }}" 
                           class="text-decoration-none">{{ post.category.name }}</a>
                    </li>
                    {% endif %}
                    {% if post.tags %}
                    <li>
                        <i class="fas fa-tags text-primary"></i>
                        <strong>标签：</strong>
                        {% for tag in post.tags %}
                        <a href="{{ url_for('main.tag_posts', slug=tag.slug) }}" 
                           class="badge bg-secondary text-decoration-none me-1">
                            {{ tag.name }}
                        </a>
                        {% endfor %}
                    </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
/* 数学公式样式 */
.math-inline {
    display: inline-block;
    margin: 0 2px;
}

.math-block {
    text-align: center;
    margin: 1rem 0;
    padding: 1rem;
    background-color: #f8f9fa;
    border-radius: 0.375rem;
    border: 1px solid #e9ecef;
}

/* MathJax 公式样式调整 */
.MathJax {
    font-size: 1.1em;
}

.MathJax_Display {
    margin: 1rem 0;
}
</style>
{% endblock %}

{% block extra_js %}
<!-- MathJax 数学公式支持 -->
<script>
window.MathJax = {
    tex: {
        inlineMath: [['\\(', '\\)'], ['$', '$']],
        displayMath: [['\\[', '\\]'], ['$$', '$$']],
        processEscapes: true,
        processEnvironments: true,
        processRefs: true
    },
    options: {
        skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
        ignoreHtmlClass: 'tex2jax_ignore',
        processHtmlClass: 'tex2jax_process'
    },
    startup: {
        ready: function () {
            console.log('MathJax is loaded, but not yet initialized');
            MathJax.startup.defaultReady();
            console.log('MathJax is initialized, and the initial typeset is queued');
        }
    }
};
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

<script>
// 确保页面加载完成后重新渲染数学公式
document.addEventListener('DOMContentLoaded', function() {
    if (window.MathJax) {
        MathJax.typesetPromise().catch((err) => {
            console.error('MathJax渲染错误:', err);
        });
    }
});

function shareToWeibo() {
    const url = encodeURIComponent(window.location.href);
    const title = encodeURIComponent(document.title);
    window.open(`https://service.weibo.com/share/share.php?url=${url}&title=${title}`, '_blank');
}

function shareToWechat() {
    // 这里可以集成微信分享SDK
    alert('请复制链接分享到微信');
}

function copyLink() {
    navigator.clipboard.writeText(window.location.href).then(() => {
        alert('链接已复制到剪贴板');
    });
}
</script>
{% endblock %}

